package com.zcsy.commons.util;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

import com.zcsy.commons.constants.Constants;
import com.zcsy.manage.model.pay.weixin.PayOrderRequest;
import com.zcsy.manage.model.pay.weixin.PayOrderResultRequest;

public class SignUtil {
    /**
     * 生成签名
     */
    public static String createSign(PayOrderRequest request) {
        //根据规则创建可排序的map集合
        SortedMap<String, String> packageParams = new TreeMap<String, String>();
        packageParams.put("appid", request.getAppid());
        packageParams.put("body", request.getBody());
        packageParams.put("mch_id", request.getMch_id());
        packageParams.put("nonce_str", request.getNonce_str());
        packageParams.put("notify_url", request.getNotify_url());
        packageParams.put("out_trade_no", request.getOut_trade_no());
        packageParams.put("spbill_create_ip", request.getSpbill_create_ip());
        packageParams.put("trade_type", request.getTrade_type());
        packageParams.put("total_fee", request.getTotal_fee());

        StringBuffer sb = new StringBuffer();
        Set es = packageParams.entrySet();//字典序
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            String v = (String) entry.getValue();
            //为空不参与签名、参数名区分大小写
            if (null != v && !"".equals(v) && !"sign".equals(k)
                    && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
        //第二步拼接key，key设置路径：微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置
        sb.append("key=" + Constants.KEY);
        String sign = MD5Util.MD5Encode(sb.toString(), "utf-8")
                .toUpperCase();//MD5加密
        return sign;
    }
    
    public static String createResultSign(PayOrderResultRequest request) {
        
        //根据规则创建可排序的map集合
        SortedMap<String, String> packageParams = new TreeMap<String, String>();
        packageParams.put("appid", request.getAppid());
        packageParams.put("mch_id", request.getMch_id());
        packageParams.put("nonce_str", request.getNonce_str());
        packageParams.put("out_trade_no", request.getOut_trade_no());

        StringBuffer sb = new StringBuffer();
        Set es = packageParams.entrySet();//字典序
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            String v = (String) entry.getValue();
            //为空不参与签名、参数名区分大小写
            if (null != v && !"".equals(v) && !"sign".equals(k)
                    && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
        //第二步拼接key，key设置路径：微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置
        sb.append("key=" + Constants.KEY);
        String sign = MD5Util.MD5Encode(sb.toString(), "utf-8")
                .toUpperCase();//MD5加密
        return sign;
    }
}
